import axios from 'axios'
import router from '@/router'
import { Request_White_List } from "@/const"

// baseurl配置
const cfg = {
    dev: 'api',
    pro: 'api',
};
let base_url = '';
switch (process.env.NODE_ENV) {
    case "development":
        base_url = cfg.dev;
        break;
    case "testing":
        base_url = cfg.dev;
        break;
    case "production":
        base_url = cfg.pro;
        break;
}

// axios拦截器等配置
const _axios = axios.create({
    baseURL: base_url,
    withCredentials: true
});

_axios.interceptors.request.use(
    (config) => {
        if(Request_White_List.indexOf(config.url) < 0) {
            config.headers['Authorization'] = localStorage.getItem('user')
        }
        return config;
    }, (error) => {
        return Promise.reject(error);
    }
);

_axios.interceptors.response.use(
    (response) => {
         // 接口返回错误
        if(response.data.code !== 0) {
            vm.$toast.fail(response.data.message);
        }
        return response;
    }, (error) => {
        vm.$toast.fail('系统异常');
        return Promise.reject(error);
    }
);

export const baseURL = base_url
export const service = _axios